package tolgahan.albayrak.as3.reflection
{
	import flash.utils.getDefinitionByName;

	public class MethodInfo extends ParameterizedInfo
	{
		protected var _returnType:Type;
		
		public function MethodInfo(xml:XML, baseType:Type, memberIndex:uint)
		{
			super(xml, baseType, memberIndex);
			var t:String = xml.@returnType[0];
			if(t == "*")
				t = "Object";
			if(t != "void")
				_returnType = Type.GetType(getDefinitionByName(t) as Class);
		}
		
		public function get returnType():Type
		{
			return _returnType;
		}

		
		public function invoke(source:Object, ...args):*
		{
			if((source is _baseType.classReference) && validate(args))
			{
				return source[_name].apply(source, getParameters(args));
			}
		}
		
		public function toString():String
		{
			if(_returnType)
				return "public function " + _name + "(" + _parameters + "):" + _returnType + "\n\t\t{\n\t\t\treturn default(" + _returnType + ")\n\t\t}";
			return "public function " + _name + "(" + _parameters + "):void\n\t\t{\n\t\t}";
		}
	}
}